package com.ycMc.controller;

import com.ycMc.bean.request.SupplierInfoRequest;
import com.ycMc.bean.response.ListResponse;
import com.ycMc.bean.response.ObjectResponse;
import com.ycMc.common.bean.Response;
import com.ycMc.domain.Supplier;
import com.ycMc.service.SupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by libo2 on 2017/12/12.
 */
@RequestMapping("api/supplier")
@Controller
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @RequestMapping("insert")
    @ResponseBody
    public Response insert(SupplierInfoRequest request){
        Response response = new Response();

        Supplier supplier = new Supplier();

        BeanUtils.copyProperties(request,supplier);

        try{
            supplierService.insert(supplier);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }




    @RequestMapping("update")
    @ResponseBody
    public Response update(SupplierInfoRequest request){
        Response response = new Response();

        Supplier supplier = new Supplier();

        BeanUtils.copyProperties(request,supplier);

        try{
            supplierService.update(supplier);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }



    @RequestMapping("delete")
    @ResponseBody
    public Response delete(SupplierInfoRequest request){
        Response response = new Response();

        try{
            supplierService.delete(request.getId());
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }


    @RequestMapping("selectAll")
    @ResponseBody
    public ListResponse selectAll(SupplierInfoRequest request){
        ListResponse response = new ListResponse();

        try{
            List<Supplier> supplier =  supplierService.selectAll();
            response.setCommonList(supplier);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("success");
        }
        catch (Exception e){
            response.setStatusCode(Response.FAILED);
            response.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("selectOne")
    @ResponseBody
    public ObjectResponse selectOne(SupplierInfoRequest request)
    {ObjectResponse response = new ObjectResponse();


        response.setObject(supplierService.selectOne(request.getId()));

        response.setStatusCode(Response.SUCCESS);
        response.setMsg("suucess");

        return  response;
        
    }

}
